<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

class UserController extends Zend_Controller_Action{
    public function indexAction(){
        $auth 	 = Zend_Auth::getInstance();
		$request = $this->getRequest();

        if( $request->isXmlHttpRequest() || $request->format == 'json' ){
			$this->_forward( 'json' );
		}
        else if( $auth->hasIdentity() ){
            $this->_redirect( '/user/profile' );
        }
        else{
            $this->_redirect( '/user/login' );
        }
        exit;
    }

	public function jsonAction(){
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$this->_helper->json( $this->_helper->user()->toArray() );
	}

    public function annotationAction(){
		if( ! Zend_Auth::getInstance()->hasIdentity() ){
			$this->_forward( 'login' );
		}

		$user = $this->_helper->user();
        $annotation = new Default_Model_Annotation( array('user_id' => $user->id ));

		$this->view->annotations = $annotation->fetchAll();
		$this->view->user      = $user;
    }

    public function profileAction(){
		if( ! Zend_Auth::getInstance()->hasIdentity() ){
			$this->_forward( 'login' );
		}
    }

    public function logoutAction(){
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();

		$this->_redirect( '/' );
    }

	public function loginAction(){
		$request = $this->getRequest();

		$auth = Zend_Auth::getInstance();
		$endpoint = $request->endpoint;

		$form = new Default_Form_Login();

		if( $auth->hasIdentity() ){
			$this->_redirect( '/user/profile' );
		}
		else if( ( $request->isPost()  )
				|| ( null !== $request->openid_mode && $request->openid_mode == 'id_res' ) ){

			$endpoint = trim( $request->endpoint );
			$element  = 'endpoint';

			if( null !== $request->endpoint_custom && $request->endpoint_custom !== ''
			   || null !== $request->openid_sreg_email && $request->openid_sreg_email !== ''){
				// if a custom endpoint is chosen, we rely on openid 1.1
				$endpoint = trim( $request->endpoint_custom );

				$extension = new Zend_OpenId_Extension_Sreg( array(
					'firstname'	=> false,
					'email'		=> false,
				));

				$adapter   = new Zend_Auth_Adapter_OpenId( $endpoint, null, null, null, $extension );
			}
			else{
				// Google, Yahoo do openid 2.0 ( sort of ) Our custom _Ax is a bit of a hack on top.
				$extension = new At_OpenId_Extension_Ax( array(
					'firstname'	=> false,
					'email'		=> true,
				));

				$adapter   = new At_OpenId_Adapter( $endpoint, null, null, null, $extension );
			}

			$result    = $auth->authenticate( $adapter );

			if($result->getCode() !== 1){
				At_Debug::log( $result, 'AUTH result');

				$form->getElement( $element )->setErrors( array('Could not connect to your identity-provider'), false);
			}
			else if( $result->isValid() ){
				$values = (object) $extension->getProperties();

				$user = $this->_helper->user();

				$user->nickname   = isset( $values->firstname ) ? $values->firstname : $values->email;
				$user->fullname   = isset( $values->firstname ) ? $values->firstname : $values->email;;
				$user->email      = $values->email;
				$user->identifier = $request->openid_identity;

				$user->save();


				$this->_redirect( '/user/profile' );
			}
		}
		else if( null !== $request->openid_mode ){
			if( $request->openid_mode == 'cancel' ){
				$form->getElement( 'endpoint' )->setErrors( array('You canceled the login proces with the remote party!') );
			}
			else{
				$form->getElement( 'endpoint' )->setErrors( array('An unknown error occured while validating your account') );
			}
		}

		$this->view->form = $form;

	}
}
